package com.lfy.Factor;

import com.lfy.Factor.Impl.Mon1;
import com.lfy.Factor.Impl.Mon2;
import com.lfy.Factor.Impl.Mon3;
import com.lfy.Factor.Impl.Mon4;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.locks.ReentrantLock;
/**
 * Mon策略
 *
 * @Author lfy
 * @Date 2023/4/12 19:15
 * @Email 1316547530@qq.com
 * @Blog www.lvfeiy.top
 */
public class MonFactorBean {
    private static MonFactorBean bean = new MonFactorBean();
    private static List<MonFactorParse> list = new ArrayList<>();
    static {
        Properties properties = new Properties();
        InputStream bean = MonFactorParse.class.getResourceAsStream("/beanFactor.properties");
        try {
            properties.load(bean);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (Object o : properties.keySet()) {
            try {
                list.add((MonFactorParse)  Class.forName((String) properties.get(o)).newInstance());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    private MonFactorBean(){}
    //单例
    public static MonFactorBean getInstence(){
        return bean;
    }
    //返回实现类
    public MonFactorParse getMon(String code){
        MonFactorParse p = null;
        for (MonFactorParse monFactorParse : list) {
            if(monFactorParse.Check(code)){
                //优先级
                if(p == null || monFactorParse.GetPriority() > p.GetPriority()){
                    p = monFactorParse;
                }

            }
        }
        if(p==null){
            throw new RuntimeException("没有找到策略");
        }
        return p;
    }

}
